package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Like;
import com.example.mapper.LikeMapper;
import com.example.service.ILikeService;
import com.example.service.IUserService;
import com.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王鹤蒲
 * @since 2023-09-07
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements ILikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private IUserService iUserService;

    public R saveLike(Like like) {
        LocalDateTime createTime = LocalDateTime.now();
        like.setCreateTime(createTime).setCreateBy(iUserService.getCurrentLoginUser().getUserName());
        likeMapper.insert(like);
        return R.success();
    }

    public R updateLike(Like like) {
        LocalDateTime updateTime = LocalDateTime.now();
        like.setUpdateBy(iUserService.getCurrentLoginUser().getUserName()).setUpdateTime(updateTime);
        likeMapper.updateById(like);
        return R.success();
    }

    public R deleteById(Integer id) {
        LocalDateTime deleteTime = LocalDateTime.now();
        LambdaUpdateWrapper<Like> lambdaUpdateWrapper = new LambdaUpdateWrapper<Like>();
        lambdaUpdateWrapper.eq(Like::getId, id);
        lambdaUpdateWrapper.set(Like::getDelFlag, "1");
        lambdaUpdateWrapper.set(Like::getUpdateBy, iUserService.getCurrentLoginUser().getUserName());
        lambdaUpdateWrapper.set(Like::getUpdateTime, deleteTime);
        likeMapper.update(new Like(), lambdaUpdateWrapper);
        return R.success();
    }

    public R deleteByIds(Integer[] ids) {
        LocalDateTime deleteTime = LocalDateTime.now();
        LambdaUpdateWrapper<Like> lambdaUpdateWrapper = new LambdaUpdateWrapper<Like>();
        for (Integer id : ids) {
            lambdaUpdateWrapper.clear();
            lambdaUpdateWrapper.eq(Like::getId, id);
            lambdaUpdateWrapper.set(Like::getDelFlag, "1");
            lambdaUpdateWrapper.set(Like::getUpdateBy,iUserService.getCurrentLoginUser().getUserName());
            lambdaUpdateWrapper.set(Like::getUpdateTime, deleteTime);
            likeMapper.update(new Like(), lambdaUpdateWrapper);
        }
        return R.success();
    }

    public R findPage(int currentPage, int pageSize, String name) {
        Page<Like> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Like::getDelFlag, "0");
        return R.success(likeMapper.selectPage(page, lambdaQueryWrapper));
    }


    public R findOne(Integer id) {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<Like>();
        lambdaQueryWrapper.eq(Like::getDelFlag, "0");
        lambdaQueryWrapper.eq(Like::getId, id);
        return R.success(likeMapper.selectOne(lambdaQueryWrapper));
    }

    public R findAll() {
        LambdaQueryWrapper<Like> lambdaQueryWrapper = new LambdaQueryWrapper<Like>();
        lambdaQueryWrapper.eq(Like::getDelFlag, "0");
        return R.success(likeMapper.selectList(lambdaQueryWrapper));
    }

}
